package GUI;

import Run_System.Exercises;
import Run_System.ExervisesFactory;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class CustomPanel extends JPanel {
    private static final int DEFAULT_WIDTH = 400;
    private static final int DEFAULT_HEIGHT = 300;
    private int arr[] = {100,0,50,50};  //供生成算式模块使用，包含了算式的最大值、最小值、个数以及加减法比例
    protected Exercises exercises;

    public CustomPanel(){
        setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
        setLayout(new BorderLayout());

        JPanel outPanel = new JPanel();

        JPanel inputPanel = new JPanel(new GridLayout(3,2,5,20));
        inputPanel.setPreferredSize(new Dimension(150,150));

        JLabel max_Label = new JLabel("算式最大范围",SwingConstants.RIGHT);  //输入算式最大范围
        JTextField max_jt = new JTextField(5);

        JLabel min_Label = new JLabel("算式最小范围",SwingConstants.RIGHT); //输入算式最小范围
        JTextField min_jt = new JTextField(5);

        JLabel size_Label = new JLabel("习题数量",SwingConstants.RIGHT);   //输入习题数量
        JTextField size_jt = new JTextField(5);

        inputPanel.add(max_Label);
        inputPanel.add(max_jt);
        inputPanel.add(min_Label);
        inputPanel.add(min_jt);
        inputPanel.add(size_Label);
        inputPanel.add(size_jt);

        JButton insertButton = new JButton("开始做题吧，小明😀");//获取输入数据
        insertButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {       //存放到arr数组中
                arr[0] = Integer.parseInt(max_jt.getText());
                arr[1] = Integer.parseInt(min_jt.getText());
                arr[2] = Integer.parseInt(size_jt.getText());
                DisplayExercisesPanel panel = new DisplayExercisesPanel(ExervisesFactory.generateExercises(ExervisesFactory.MIX_EXERCISES,arr[2],arr[3]), new ArrayList<Integer>());
                removeAll();
                add(panel);
                validate();
            }
        });
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(insertButton);

        JSlider slider = new JSlider(0,100,50);  //添加滑块，用户可滑动滑块选择加减法算式的比例
        slider.setMajorTickSpacing(25);
        slider.setPaintTicks(true);
        slider.setPaintLabels(true);
        slider.setSnapToTicks(true);
        slider.addChangeListener(new ChangeListener() {   //用户滚动时获取加减法比例
            @Override
            public void stateChanged(ChangeEvent e) {
                JSlider source = (JSlider) e.getSource();
                arr[3] = source.getValue();
            }
        });
        JPanel sliderPanel = new JPanel();
        JLabel label = new JLabel("滚动选择加减法的比例");
        slider.add(label);
        sliderPanel.add(slider);
        add(sliderPanel,BorderLayout.NORTH);

        add(buttonPanel,BorderLayout.SOUTH);
        outPanel.add(inputPanel);
        add(outPanel,BorderLayout.CENTER);

    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setSize(852,543);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.add(new CustomPanel());
        frame.setVisible(true);
    }
}
